Imports System.Drawing.Text
Public Class FormMessage
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal user As String, ByVal thisUser As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Tag = user
        sUser = thisUser
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents cmbFont As System.Windows.Forms.ComboBox
    Friend WithEvents cmbSize As System.Windows.Forms.ComboBox
    Friend WithEvents cmdBold As System.Windows.Forms.Button
    Friend WithEvents cmdItalic As System.Windows.Forms.Button
    Friend WithEvents cmdUnderline As System.Windows.Forms.Button
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents rtChat As System.Windows.Forms.RichTextBox
    Friend WithEvents cmdSend As System.Windows.Forms.Button
    Friend WithEvents fgColor As System.Windows.Forms.ColorDialog
    Friend WithEvents bgColor As System.Windows.Forms.ColorDialog
    Friend WithEvents rtLog As System.Windows.Forms.RichTextBox
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.cmbFont = New System.Windows.Forms.ComboBox
        Me.cmbSize = New System.Windows.Forms.ComboBox
        Me.cmdBold = New System.Windows.Forms.Button
        Me.cmdItalic = New System.Windows.Forms.Button
        Me.cmdUnderline = New System.Windows.Forms.Button
        Me.cmdSend = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.fgColor = New System.Windows.Forms.ColorDialog
        Me.rtChat = New System.Windows.Forms.RichTextBox
        Me.StatusBar1 = New System.Windows.Forms.StatusBar
        Me.bgColor = New System.Windows.Forms.ColorDialog
        Me.rtLog = New System.Windows.Forms.RichTextBox
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'cmbFont
        '
        Me.cmbFont.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmbFont.Location = New System.Drawing.Point(8, 184)
        Me.cmbFont.Name = "cmbFont"
        Me.cmbFont.Size = New System.Drawing.Size(104, 21)
        Me.cmbFont.TabIndex = 2
        Me.cmbFont.Text = "Arial"
        '
        'cmbSize
        '
        Me.cmbSize.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmbSize.Items.AddRange(New Object() {"8", "10", "11", "12", "14", "16", "18", "20", "24", "28", "36", "48", "72"})
        Me.cmbSize.Location = New System.Drawing.Point(112, 184)
        Me.cmbSize.Name = "cmbSize"
        Me.cmbSize.Size = New System.Drawing.Size(40, 21)
        Me.cmbSize.TabIndex = 3
        Me.cmbSize.Text = "8"
        '
        'cmdBold
        '
        Me.cmdBold.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmdBold.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdBold.Location = New System.Drawing.Point(152, 184)
        Me.cmdBold.Name = "cmdBold"
        Me.cmdBold.Size = New System.Drawing.Size(21, 21)
        Me.cmdBold.TabIndex = 4
        Me.cmdBold.Text = "B"
        '
        'cmdItalic
        '
        Me.cmdItalic.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmdItalic.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdItalic.Location = New System.Drawing.Point(176, 184)
        Me.cmdItalic.Name = "cmdItalic"
        Me.cmdItalic.Size = New System.Drawing.Size(21, 21)
        Me.cmdItalic.TabIndex = 5
        Me.cmdItalic.Text = "I"
        '
        'cmdUnderline
        '
        Me.cmdUnderline.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.cmdUnderline.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdUnderline.Location = New System.Drawing.Point(200, 184)
        Me.cmdUnderline.Name = "cmdUnderline"
        Me.cmdUnderline.Size = New System.Drawing.Size(21, 21)
        Me.cmdUnderline.TabIndex = 6
        Me.cmdUnderline.Text = "U"
        '
        'cmdSend
        '
        Me.cmdSend.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cmdSend.Location = New System.Drawing.Point(304, 208)
        Me.cmdSend.Name = "cmdSend"
        Me.cmdSend.Size = New System.Drawing.Size(64, 32)
        Me.cmdSend.TabIndex = 1
        Me.cmdSend.Text = "Send"
        '
        'Button5
        '
        Me.Button5.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Button5.Location = New System.Drawing.Point(304, 240)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(64, 32)
        Me.Button5.TabIndex = 8
        Me.Button5.TabStop = False
        Me.Button5.Text = "TBD"
        '
        'rtChat
        '
        Me.rtChat.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.rtChat.Location = New System.Drawing.Point(8, 208)
        Me.rtChat.Name = "rtChat"
        Me.rtChat.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical
        Me.rtChat.Size = New System.Drawing.Size(296, 64)
        Me.rtChat.TabIndex = 0
        Me.rtChat.Text = ""
        '
        'StatusBar1
        '
        Me.StatusBar1.Cursor = System.Windows.Forms.Cursors.Hand
        Me.StatusBar1.Location = New System.Drawing.Point(0, 272)
        Me.StatusBar1.Name = "StatusBar1"
        Me.StatusBar1.Size = New System.Drawing.Size(368, 22)
        Me.StatusBar1.TabIndex = 11
        '
        'rtLog
        '
        Me.rtLog.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.rtLog.Location = New System.Drawing.Point(8, 8)
        Me.rtLog.Name = "rtLog"
        Me.rtLog.ReadOnly = True
        Me.rtLog.Size = New System.Drawing.Size(352, 168)
        Me.rtLog.TabIndex = 12
        Me.rtLog.Text = ""
        '
        'Timer1
        '
        Me.Timer1.Interval = 500
        '
        'FormMessage
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 294)
        Me.Controls.Add(Me.rtLog)
        Me.Controls.Add(Me.StatusBar1)
        Me.Controls.Add(Me.rtChat)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.cmdSend)
        Me.Controls.Add(Me.cmdUnderline)
        Me.Controls.Add(Me.cmdItalic)
        Me.Controls.Add(Me.cmdBold)
        Me.Controls.Add(Me.cmbSize)
        Me.Controls.Add(Me.cmbFont)
        Me.Name = "FormMessage"
        Me.Text = "STIMPy! > Chatting with "
        Me.ResumeLayout(True)

    End Sub

#End Region
    Private sUser As String = "Unknown"
    Private fontCollection As System.Drawing.Text.InstalledFontCollection
    Private sAd As String = "Click here for STIMPy! News"
    Private sAdLink As String = "http://www.codenamestimpy.com"
    Declare Auto Function FlashIt Lib "user32" _
      Alias "FlashWindow" (ByVal hwnd As Integer, _
       ByVal bInvert As Integer) As Integer
    Private iNumFlashes As Int32
    Private isFront As Boolean
    Private oMsgArray As ArrayList
    Private iMsgArrIndex As Int32
    'Private cLastMsgScroll As String
    Private cLastMsgAction As String

    Public Property User() As String
        Get
            User = sUser
        End Get
        Set(ByVal Value As String)
            sUser = Value
        End Set
    End Property

    Private Sub FormMessage_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
        Dim r As New Random
        'Dim tmpAdArr As Object =frmRef.AdSys.GetTextAd()
        fontCollection = New System.Drawing.Text.InstalledFontCollection
        Dim fFamily As FontFamily

        Me.Text = Me.Text + Tag
        'sAd=tmpAdArr(0)
        'sAdLink=tmpAdArr(1)
        StatusBar1.Text = sAd
        For Each fFamily In fFamily.Families
            cmbFont.Items.Add(fFamily.Name)
        Next
        oMsgArray = New ArrayList
        oMsgArray.Add(vbNullString)
    End Sub

    Private Sub rtChat_KeyUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtChat.KeyUp
        'MsgBox(e.KeyCode.ToString + ":" + e.KeyData.ToString + ":" + e.KeyValue.ToString)
        If e.KeyValue = 13 And Not e.Shift Then
            If rtChat.Text <> vbNullString And _
              rtChat.Text.Substring(rtChat.TextLength - 1) = Chr(10) Then
                'MsgBox(rtChat.Text.Substring(rtChat.TextLength - 1) = vbNewLine)
                rtChat.Text = rtChat.Text.Substring(0, rtChat.TextLength - 1)
                rtChat.SelectionStart = rtChat.TextLength
                rtChat.SelectionLength = 0
                cmdSend_Click(sender, e)
                e.Handled = True
                'Call the sub for Send here
            End If
        End If
        If oMsgArray.Count > 1 And (e.KeyCode.ToString = "Up" _
          Or e.KeyCode.ToString = "Down") Then
            'If rtChat.Text.Length = 0 Or rtChat.Text = cLastMsgScroll Then
            Select Case e.KeyCode.ToString
                Case "Up"
                    If Not cLastMsgAction Is Nothing Then iMsgArrIndex -= 1
                    If iMsgArrIndex < 0 Then iMsgArrIndex = oMsgArray.Count - 1
                    rtChat.Text = oMsgArray.Item(iMsgArrIndex)
                    rtChat.SelectionStart = rtChat.Text.Length
                    rtChat.ScrollToCaret()

                Case "Down"
                    iMsgArrIndex += 1
                    If iMsgArrIndex > oMsgArray.Count - 1 Then iMsgArrIndex = 0
                    If cLastMsgAction Is Nothing Then iMsgArrIndex += 1
                    rtChat.Text = oMsgArray.Item(iMsgArrIndex)
                    rtChat.SelectionStart = rtChat.Text.Length
                    rtChat.ScrollToCaret()
            End Select
            cLastMsgAction = e.KeyCode.ToString
        End If
    End Sub

    Private Sub rtLog_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles rtChat.LinkClicked
        System.Diagnostics.Process.Start(e.LinkText)
    End Sub

    Private Sub StatusBar1_MouseUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles StatusBar1.MouseUp
        Process.Start(sAdLink)
    End Sub

    Private Sub cmdSend_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles cmdSend.Click
        If rtChat.Text.Length = 0 Then Exit Sub
        sktMain.Send("MSGTO_" + Tag + ":" + rtChat.Text)
        frmMain.playSound(App.SoundPath + "MessageSent")
        If Not rtLog.TextLength = 0 Then rtLog.Text += vbNewLine
        'rtLog.Rtf += rtChat.Rtf
        rtLog.Text += sUser + ": " + rtChat.Text
        rtLog.SelectionStart = rtLog.Text.Length
        rtLog.ScrollToCaret()
        oMsgArray.Add(rtChat.Text)
        iMsgArrIndex = oMsgArray.Count - 1
        'Me.cLastMsgScroll = vbNullString
        rtChat.Text = vbNullString
        cLastMsgAction = Nothing
    End Sub

    Private Sub cmbFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFont.SelectedIndexChanged
        If rtChat.SelectionLength > 0 Then
            'rtChat.SelectionFont = cmbFont.SelectedText
        End If
    End Sub
    Public Sub DoInfo(ByVal user As String, ByVal msg As String)
        If Tag Is Nothing Or Tag = vbNullString Then
            Tag = user
        End If
        With rtLog
            If Not .Text = vbNullString Then
                .Text += vbNewLine
                .SelectionStart = .Text.Length
                .ScrollToCaret()
            End If
            .Text += user + ": " + msg
        End With
        'If Me.Text.IndexOf(Tag) < 0 Then Me.Text = "Codename: STIMPy! - Chatting with " + Tag
    End Sub

    Private Sub FormMessage_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        frmMain.msgWinArr.Remove(Me)
    End Sub

    Private Sub rtLog_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles rtLog.LostFocus
        rtLog.SelectionStart = rtLog.Text.Length
    End Sub

    Public Sub Flash(ByVal numTimes As Int32)
        iNumFlashes = numTimes
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If iNumFlashes = 0 Then
            Timer1.Enabled = False
            Exit Sub
        Else
            iNumFlashes -= 1
        End If
        Dim iInt As Integer
        'imports flashwindow.api imports the api class.
        iInt = FlashIt(Me.Handle.ToInt32, 1)
    End Sub

    Private Sub FormMessage_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
        isFront = True
    End Sub

    Private Sub FormMessage_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LostFocus
        isFront = False
    End Sub
    Public ReadOnly Property isTopWindow() As Boolean
        Get
            Return isFront
        End Get
    End Property
End Class

